﻿using System.ComponentModel.Composition;
using System.Dynamic;
using System.Web.Mvc;
using Mefisto.Core.Caching.Contracts;
using Mefisto.Core.Logging.Contracts;
using Mefisto.Core.Resources.Contracts;
using System;
using System.Collections.Generic;

namespace Mefisto.Core.Mvc.Controllers
{
    [Export(typeof(MefistoController))]
    public class MefistoController : Controller
    {
        #region Imported Fields

        [Import]
        private IResourceProvider _resourceProvider;

        [Import]
        private ICacheProvider _cacheProvider;

        [Import]
        private ILoggingProvider _loggingProvider;

        #endregion

        #region Fields

        private ExpandoObject _translationBag;

        private ExpandoObject _loggingBag;

        #endregion

        #region Properties

        public dynamic TranslationBag
        {
            get { return _translationBag ?? (_translationBag = new ExpandoObject()); }
        }

        public ICacheProvider CacheProvider
        {
            get { return _cacheProvider; }
        }

        public dynamic LoggingBag
        {
            get { return _loggingBag ?? (_loggingBag = new ExpandoObject()); }
        }

        /// <summary>
        /// Code de la culture en cours
        /// </summary>
        public string CultureCode
        {
            get
            {
                return Session["CultureCode"].ToString();
            }
            set
            {
                Session["CultureCode"] = value;
            }
        }

        /// <summary>
        /// Code de l'application en cours
        /// </summary>
        public string ApplicationCode
        {
            get
            {
                return Session["ApplicationCode"].ToString();
            }
            set
            {
                Session["ApplicationCode"] = value;
            }
        }

        #endregion

        #region Service Methods

        public T Import<T>()
        {
            return DependencyResolver.Current.GetService<T>();
        }

        public object Import(Type typeName)
        {
            return DependencyResolver.Current.GetService(typeName);
        }

        public IEnumerable<T> ImportMany<T>()
        {
            return DependencyResolver.Current.GetServices<T>();
        }

        public IEnumerable<object> ImportMany(Type typeName)
        {
            return DependencyResolver.Current.GetServices(typeName);
        }

        #endregion

        #region Translation Methods

        public string GetTranslation(string resourceCode)
        {
            _resourceProvider.TranslationBag = TranslationBag;
            return _resourceProvider.GetTranslation(resourceCode, CultureCode, ApplicationCode);
        }

        public void ClearTranslationBag()
        {
            _translationBag = null;
        }

        #endregion

        #region Log Methods

        public void LogMessage(string message)
        {
            _loggingProvider.LogMessage(message, ApplicationCode);
        }

        public void LogError(Exception error)
        {
            _loggingProvider.LogError(error, ApplicationCode);
        }

        #endregion
    }
}